package com.tecklab.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tecklab.demo.common.BizException;
import com.tecklab.demo.dto.EmployeeDTO;
import com.tecklab.demo.entity.DepartmentEntity;
import com.tecklab.demo.entity.EmployeeEntity;
import com.tecklab.demo.mapper.DepartmentMapper;
import com.tecklab.demo.mapper.EmployeeMapper;
import com.tecklab.demo.param.EmployeeAddParam;
import com.tecklab.demo.param.EmployeeModifyParam;
import com.tecklab.demo.service.EmployeeService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * * 用户服务实现类
 */
@Service  // 重要：添加@Service注解 , 标记为Spring服务组件
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    @SneakyThrows
    private EmployeeEntity convertDto2Entity(EmployeeDTO dto) {
        EmployeeEntity employeeEntity = new EmployeeEntity();
        BeanUtils.copyProperties(employeeEntity, dto);
        return employeeEntity;
    }

    @SneakyThrows
    private EmployeeDTO convertEntity2Dto(EmployeeEntity employeeEntity) {
        EmployeeDTO dto = new EmployeeDTO();
        BeanUtils.copyProperties(dto, employeeEntity);
        return dto;
    }


    //
    @Override
    public int create(EmployeeAddParam addParam) {

        EmployeeEntity employee = new EmployeeEntity();
        employee.setName(addParam.getName());
        employee.setDepartmentId(addParam.getDepartmentId());
        return employeeMapper.insert(employee);

    }

    @Override
    public int modify(EmployeeModifyParam modifyParam) {

        EmployeeEntity employee = new EmployeeEntity();
        employee.setName(modifyParam.getName());
        employee.setId(modifyParam.getId());
        employee.setDepartmentId(modifyParam.getDepartmentId());
        return employeeMapper.insert(employee);

    }

    @Override
    public EmployeeDTO getById(Integer id) {

        LambdaQueryWrapper<EmployeeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EmployeeEntity::getId, id);

        EmployeeEntity employeeEntity = employeeMapper.selectById(id);
        if (employeeEntity == null) {
            throw new BizException("数据不存在");
        }
        // 2. 转换为DTO
        EmployeeDTO employeeDTO = this.convertEntity2Dto(employeeEntity);

        // 3. 新增：设置部门名称
        if (employeeEntity.getDepartmentId() != null) {
            DepartmentEntity department = departmentMapper.selectById(employeeEntity.getDepartmentId());
            if (department != null) {
                employeeDTO.setDepartmentName(department.getName());
            }
        }
        //转换并返回
        return employeeDTO;
    }

    @Override
    //查询所有员工（可能在不同部门）
    public List<EmployeeDTO> getList() {
        // null参数表示不添加任何查询条件，获取全部数据
        List<EmployeeEntity> entityLists = employeeMapper.selectList(null);
        //用于接收转换后的employeeDTO
        List<EmployeeDTO> employeeDTOList = new ArrayList<>();

        List<DepartmentEntity> departmentEntityList = departmentMapper.selectList(null);
        // 创建一个HashMap用于存储部门ID和部门名称的映射关系
        Map<Integer, String> map = new HashMap<>();

        for (DepartmentEntity departmentEntity : departmentEntityList) {
            //
            map.put(departmentEntity.getId(), departmentEntity.getName());
        }

        //增强for循环,遍历
        for (EmployeeEntity employee : entityLists) {

            EmployeeDTO employeeDTO = this.convertEntity2Dto(employee);
            employeeDTO.setDepartmentName(map.get(employee.getDepartmentId()));

            employeeDTOList.add(employeeDTO);

        }
        return employeeDTOList;
    }

    @Override
    public List<EmployeeDTO> getByDepartmentId(Integer departmentId) {

        String deptName = departmentMapper.selectById(departmentId).getName();

        LambdaQueryWrapper<EmployeeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EmployeeEntity::getDepartmentId, departmentId);
        //
        List<EmployeeEntity> entityList = employeeMapper.selectList(lambdaQueryWrapper);
        //
        List<EmployeeDTO> employeeDTOList = new ArrayList<>();
        //
        for (EmployeeEntity employee : entityList) {
            //
            EmployeeDTO employeeDTO = this.convertEntity2Dto(employee);
            employeeDTO.setDepartmentName(deptName); // 设置统一的部门名称

            employeeDTOList.add(employeeDTO);

        }
        return employeeDTOList;
    }

    @Override
    public int deleteByName(String name) {
        // StringUtils.isBlank()
        if (StringUtils.isBlank(name)) {
            throw new RuntimeException("名字不能为空");
        }

        LambdaQueryWrapper<EmployeeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EmployeeEntity::getName, name);

        return employeeMapper.delete(lambdaQueryWrapper);


    }

//    @Override
//    public EmployeeEntity deleteByName(String name) {
//        LambdaQueryWrapper<EmployeeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(EmployeeEntity::getName, name);
//
//        if (name == null) {
//            throw new RuntimeException("名字不能为空");
//        }
//        EmployeeEntity employee = employeeMapper.selectOne(lambdaQueryWrapper);
//        if (employee != null) {
//            employeeMapper.delete(lambdaQueryWrapper);
//        }
//        return employee;
//    }


}

